package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketCollect;
import com.cskaoyan.bean.PO.MarketUser;
import com.cskaoyan.bean.VO.WxCollectListVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.WxCollectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author:zhangjiaxin
 **/
@Service
public class WxCollectServiceImpl implements WxCollectService{
    @Autowired
    WxCollectMapper wxCollectMapper;


    @Transactional
    @Override
    public CommonData list(Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        //使用shiro 获取userid保存数据
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = null;
        try {
            userId = user.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
//        查询数据
        List<WxCollectListVo> list= wxCollectMapper.listCollect(type,userId);
        PageInfo<WxCollectListVo> pageInfo = new PageInfo<>(list);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Autowired
    MarketCollectMapper mapper;


    /**
     * @description: 添加收藏
     * @methodName addordelete
     * @param:  wxCollectListVo
     * @return:void
     */
    @Transactional
    @Override
    public void addordelete(WxCollectListVo wxCollectListVo) {
//        创建添加时间
        Date date = new Date();
        MarketCollect marketCollect = new MarketCollect();
        marketCollect.setAddTime(date);
        marketCollect.setUpdateTime(date);
        marketCollect.setType(wxCollectListVo.getType().byteValue());
        marketCollect.setValueId(wxCollectListVo.getValueId());
        //使用shiro 获取userid保存数据
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = null;
        try {
            userId = user.getId();
        } catch (Exception e) {
            return;
        }
        marketCollect.setUserId(userId);
        Integer id= wxCollectMapper.selectId(marketCollect);
        int abool=0;
        if(id!=null){
            abool=wxCollectMapper.deleteId(id);
            if (abool==0){
                try {
                    int j=1/0;
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    return;
                }
            }
            return;
        }

        //  marketCollect.setId(null);
//        使用逆向工程
        int i = mapper.insertSelective(marketCollect);
        if(i!=1){
            try {
                int j=1/0;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}